package com.parsley.model
{
	import com.parsley.message.DelUserMessage;
	import com.parsley.message.SaveUserMessage;
	
	import mx.collections.ArrayCollection;
	
	public class UserPM
	{
		public var name:String;
		public var email:String;
		public var department:String;
		
		[Inject]
		public var users:ArrayCollection;
		
		[MessageDispatcher]
        public var sendMessage:Function;
		
		/**
		 * Send the saving message, then the SaveUserCommand will be execute.
		 * The mapping relation is defined in AppConfig.mxml, like:
		 *  	<spicefactory:DynamicCommand type="{SaveUserCommand}" messageType="{SaveUserMessage}" />
		 */
		public function save(name:String, email:String, department:String):void
		{
			// 检验数据正确性
			if(name != '' && email != '')
			{
				var saveMsg:SaveUserMessage = new SaveUserMessage(name, email, department);
				sendMessage(saveMsg);	//派发信息
			}
			
		}
		
		/**
		 * Send the saving message, then the SaveUserCommand will be execute.
		 * The mapping relation is defined in AppConfig.mxml, like:
		 *  	<spicefactory:DynamicCommand type="{DelUserCommand}" messageType="{DelUserMessage}" />
		 */
		public function delUser(index:int):void
		{
			sendMessage(new DelUserMessage(index));
		}
		
		// 接受"SaveUserCommand"的结果，当Commandod类的处理完成后，自动派发CommandResult命令
        [CommandResult(type="com.parsley.message.SaveUserMessage")]
        public function handleSaveResult(msg:SaveUserMessage) : void {
            users.addItem(msg);
        }
        //当Commandod类的方法error处理完成后，自动派发CommandError命令
        [CommandError(type="com.parsley.message.SaveUserMessage")]
        public function handleSaveError():void {
            //message = ResourceManager.getInstance().getString('General', 'serviceFailed' );
        }
        
        // 接受"保存用户请求"的结果，当Commandod类处理完成后，自动派发CommandResult命令
        [CommandResult(type="com.parsley.message.DelUserMessage")]
        public function handleDelResult(msg:DelUserMessage) : void {
            users.removeItemAt(msg.id);
        }
        //当Commandod类的方法error处理完成后，自动派发CommandError命令
        [CommandError(type="com.parsley.message.DelUserMessage")]
        public function handleDelError():void {
            //message = ResourceManager.getInstance().getString('General', 'serviceFailed' );
        }
	}
}